package com.xtourms.client.rm.ticket;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.ContentFrame;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.client.ui.util.MoneyLabel;
import com.xtourms.shared.field.TicketField;

public class StatusManager extends Composite {

	private static StatusManagerUiBinder uiBinder = GWT
			.create(StatusManagerUiBinder.class);

	interface StatusManagerUiBinder extends UiBinder<Widget, StatusManager> {
	}

	@UiField FlexTable statusTable;
	private JSONObject ticketObj;
	private JSONArray statusArray;
	private ContentFrame parent;
	
	public StatusManager(JSONObject ticketObj, JSONArray statusArray, ContentFrame parent) {
		initWidget(uiBinder.createAndBindUi(this));
		this.statusArray = statusArray;
		this.ticketObj = ticketObj;
		this.parent = parent;
		initTable();
	}

	private void initTable() {
		int col = 0;
		statusTable.getCellFormatter().setWidth(0, col, "30px");
		statusTable.setText(0, col++, "选项");
		statusTable.getCellFormatter().setWidth(0, col, "60px");
		statusTable.setText(0, col++, "类型");
		statusTable.getCellFormatter().setWidth(0, col, "50px");
		statusTable.setText(0, col++, "底价");
		statusTable.getCellFormatter().setWidth(0, col, "50px");
		statusTable.setText(0, col++, "销售价");
		statusTable.getCellFormatter().setWidth(0, col, "50px");
		statusTable.setText(0, col++, "挂牌价");
		//statusTable.getCellFormatter().setWidth(0, col, "282px");
		//statusTable.setText(0, col++, "描述");
		statusTable.getCellFormatter().setWidth(0, col, "80px");
		statusTable.setText(0, col++, "起始日期");
		statusTable.getCellFormatter().setWidth(0, col, "80px");
		statusTable.setText(0, col++, "终止日期");
		statusTable.getRowFormatter().addStyleName(0, "result_header");

		if(null != statusArray && statusArray.size() > 0){
			for(int index = 0; index < statusArray.size(); index++){
				fillRow(index+1, (JSONObject)statusArray.get(index));
			}
		}
	}

	private void fillRow(int row, JSONObject room) {
		int col = 0;
		RadioButton rb = new RadioButton("ticketstatus");
		statusTable.setWidget(row, col++, rb);
		statusTable.setText(row, col++, ((JSONString)room.get(TicketField.STATUS_TYPE)).stringValue());
		statusTable.setWidget(row, col++, new MoneyLabel(((JSONNumber)room.get(TicketField.STATUS_BASE_PRICE)).toString()));
		statusTable.setWidget(row, col++, new MoneyLabel(((JSONNumber)room.get(TicketField.STATUS_SELL_PRICE)).toString()));
		statusTable.setWidget(row, col++, new MoneyLabel(((JSONNumber)room.get(TicketField.STATUS_ACTUAL_PRICE)).toString()));
		//statusTable.setText(row, col++, "");
		statusTable.setText(row, col++, ((JSONString)room.get(TicketField.STATUS_START_DATE)).stringValue());
		statusTable.setText(row, col++, ((JSONString)room.get(TicketField.STATUS_END_DATE)).stringValue());
		if(0 == row%2){
			statusTable.getRowFormatter().addStyleName(row, "odd");
		}else{
			statusTable.getRowFormatter().addStyleName(row, "even");
		}
	}
	
	@UiHandler("add")
	void addRoom(ClickEvent evt){
		parent.appendContent(new StatusDetailManager(ticketObj, parent, new AsyncCallback<String>() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(String result) {
				JSONObject roomObj = (JSONObject) JSONParser.parse(result);
				statusArray.set(statusArray.size(), roomObj);
				fillRow(statusArray.size(), roomObj);
				parent.resumeContent();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.ins.showError("添加票务状态发生错误："+caught.getMessage());
			}
		}));
	}
	
	@UiHandler("modify")
	void modifyRoom(ClickEvent evt){
		int index = 0;
		JSONObject statusObj = null;
		if(null != statusArray && statusArray.size() > 0){
			for(; index < statusArray.size(); index++){
				RadioButton rb = (RadioButton)statusTable.getWidget(index+1, 0);
				if(Boolean.TRUE == rb.getValue()){
					statusObj = (JSONObject)statusArray.get(index);
					break;
				}
			}
		}
		if( null != statusObj){
			final int modifyIndex = index;
			parent.appendContent(new StatusDetailManager(ticketObj, statusObj, parent, new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					MessageBox.ins.showError("添加票务状态发生错误："+caught.getMessage());
				}

				@SuppressWarnings("deprecation")
				@Override
				public void onSuccess(String result) {
					JSONObject roomObj = (JSONObject) JSONParser.parse(result);
					statusArray.set(modifyIndex, roomObj);
					fillRow(modifyIndex+1, roomObj);
					parent.resumeContent();
				}
			}));
		}
	}
	
	@UiHandler("back")
	void back(ClickEvent evt){
		parent.resumeContent();
	}
}
